#pragma once

#include "StateInvariant.h"

#include "Constraint.h"
#include "Lifeline.h"

namespace uml {

StateInvariant::StateInvariant(const QString& umlType)
    : NamedElement(umlType) {}

void StateInvariant::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    NamedElement::fromJson(json, errorList);
    InteractionFragment::fromJson(json, errorList);
    covered_ = JsonSerializable::create<Lifeline>(json.value("covered"), errorList);

    invariant_ = JsonSerializable::create<Constraint>(json.value("invariant"), errorList);

}

void StateInvariant::toJson(QJsonObject& json) {
    NamedElement::toJson(json);
    InteractionFragment::toJson(json);
    if (covered_) {
        QJsonObject covered_json;
        covered_->toJson(covered_json);
        json.insert("covered", covered_json);
    }
    if (invariant_) {
        QJsonObject invariant_json;
        invariant_->toJson(invariant_json);
        json.insert("invariant", invariant_json);
    }
}

/// <summary>
/// covered getter
/// References the Lifeline on which the StateInvariant appears.
/// </summary>
shared_ptr<Lifeline> StateInvariant::covered() const {
    return covered_;
}

/// <summary>
/// covered setter
/// References the Lifeline on which the StateInvariant appears.
/// </summary>
void StateInvariant::setCovered(shared_ptr<Lifeline> value) {
    covered_ = value;
}

/// <summary>
/// invariant getter
/// A Constraint that should hold at runtime for this StateInvariant.
/// </summary>
shared_ptr<Constraint> StateInvariant::invariant() const {
    return invariant_;
}

/// <summary>
/// invariant setter
/// A Constraint that should hold at runtime for this StateInvariant.
/// </summary>
void StateInvariant::setInvariant(shared_ptr<Constraint> value) {
    invariant_ = value;
}

} // namespace uml
